﻿using System;
using System.Collections.Generic;
using System.Text;

namespace TSVNAddinUtils
{
	public class VisualStudioConfig
	{
		private readonly RegistryAdapter _RegistryAdapter;

		public VisualStudioConfig(RegistryAdapter registryAdapter)
		{
			_RegistryAdapter = registryAdapter;
		}

		public virtual string GetInstallDir()
		{
			string installDir = (string)_RegistryAdapter.GetValue(GetKeyName64(), "InstallDir");
			if (installDir != string.Empty)
				return installDir;

			return (string)_RegistryAdapter.GetValue(GetKeyName32(), "InstallDir");
		}

		private string GetKeyName64()
		{
			return GetSoftwareKeyName64() + GetVSKeyName();
		}

		private string GetKeyName32()
		{
			return GetSoftwareKeyName32() + GetVSKeyName();
		}

		private string GetSoftwareKeyName64()
		{
			return GetSoftwareKeyName32() + @"Wow6432Node\";
		}

		private string GetSoftwareKeyName32()
		{
			return @"SOFTWARE\";
		}

		private string GetVSKeyName()
		{
			return @"Microsoft\VisualStudio\9.0";
		}

		public virtual void AddLookInFolder(string addinPath)
		{
			_RegistryAdapter.SetValue(GetLookinFolderKeyName(), addinPath, string.Empty);
		}

		public virtual void RemoveLookInFolder(string addinPath)
		{
			_RegistryAdapter.DeleteValue(GetLookinFolderKeyName(), addinPath);
		}

		private string GetLookinFolderKeyName()
		{
			string keyName = GetKeyName32();
			if (_RegistryAdapter.ExistsKey(@"SOFTWARE\Wow6432Node"))
				keyName = GetKeyName64();

			return keyName + @"\AutomationOptions\LookInFolders";
		}
	}
}
